package com.mjk.common.web.getway.restful.prototal.http;


import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.util.BeanUtils;
import com.mjk.common.util.MDCUtils;
import com.mjk.common.util.MapUtils;
import com.mjk.common.web.getway.constns.GetWayError;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import com.mjk.common.web.getway.restful.prototal.BaseGetwayInterceptor;
import com.mjk.common.web.getway.utils.BeeRoleUtils;
import com.mjk.common.web.getway.utils.RequstParaseUtils;
import com.mjk.common.web.getway.utils.ResponseUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.Objects;

/**
 * 协议转化拦截类
 * @author mjk
 * 
 */
public class GetWayHttpIntercept extends BaseGetwayInterceptor implements HandlerInterceptor{
		
	/**
	 * 该出永远放回false，不会往下走http请求的映射
	 * 在GetWayProtocalInterceptor 里面会走对应协议方法,并返回结果
	 * 放回结果的方法为 往 response 里面写入返回数据
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
							 HttpServletResponse response, Object handler) {
		MDCUtils.initMDC();//初始化traceId
		BeeRoleUtils.isComplete.set(false);
		Map<String,Object> paramsMaps = RequstParaseUtils.getMapFromRequest(request);
		GetWayInterceptorReq req = new GetWayInterceptorReq();
		req.setRestfulParams(RequstParaseUtils.getRestfulParams(request));
		req.setRequest(request);		
		req.setParams(paramsMaps);		
		req.setHeadParams(RequstParaseUtils.getHeaderFromRequest(request));	
		req.setProtocal(getWayProtocal);
		req.setResponse(response);
		req.setUploadPath(UPLOAD_PATH);
		req.setFileName(DEFAULT_FILE_NAME);
		req.setIsPre(true);
		String prototal = MapUtils.getString(req.getRestfulParams(), GetwayConsts.PROTOCOL);
		BeeResponse result = Objects.requireNonNull(GetWayInterceptorType.getGetWayInvoker(req)).invoker(chains.get(prototal), req);
		//页面请求 直接返回
		if(null!=result && result.getCode().equals(GetWayError.IS_VIEW_PRE_REQ.getCode())){
			return true;
		}
		ResponseUtils.returnGetWay(response,result);
		return false;
	}
		
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) {
		Map<String,Object> paramsMaps = RequstParaseUtils.getMapFromRequest(request);
		GetWayInterceptorReq req = new GetWayInterceptorReq();
		req.setRequest(request);		
		req.setParams(paramsMaps);
		req.setRestfulParams(RequstParaseUtils.getRestfulParams(request));
		req.setHeadParams(RequstParaseUtils.getHeaderFromRequest(request));	
		req.setProtocal(getWayProtocal);
		req.setIsPre(false);
		modelAndView = BeanUtils.toBean(chains.get(GetWayInterceptorType.VIEW.getProto()).proceed(req).getData(),ModelAndView.class);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		//do noting
	}
}
